const rewire = require('rewire')
const htmlParser = rewire('../common/htmlParser.cjs')
const {getDb} = rewire('../state.cjs')
const assert = require('assert')
const cheerio = require('cheerio')

async function mainTest() {
    const data = await getDb().select("*").from("spider_log")
        .where('id', 'a10a80b9f1a54f92aa6a8fa6952730b6')
        .queryRow()

    const html = data.html
    console.log(data.id)

    const parseRule = htmlParser.textBy('h1.page-title')

    const result = htmlParser.parse(html, parseRule)
    assert.deepEqual(
        result,
        "孕期食谱"
    )

    let rule = htmlParser.composeL(htmlParser.textBy('h1.page-title'), htmlParser.textBy('h2#steps'))
    assert.deepEqual(
        htmlParser.parse(html, rule),
        [ '孕期食谱', '孕期食谱的做法' ]
    )


    rule = htmlParser.composeO({title: htmlParser.textBy('h1.page-title'), step: htmlParser.textBy('h2#steps')})
    assert.deepEqual(
        htmlParser.parse(html, rule),
        {title: '孕期食谱', step: '孕期食谱的做法' }
    )

    rule = htmlParser.attrBy('img', 'src')
    assert.deepEqual(
        htmlParser.parse(html, rule),
        'https://i2.chuimg.com/3742dfdf641a426a9e0a49c62125343d_1280w_1706h.jpg?imageView2/2/w/660/interlace/1/q/75'
    )


    // rule = htmlParser.mapListBy('.ings tr', htmlParser.composeL(htmlParser.textBy('td.name a'), htmlParser.textBy('td.unit')))
    // console.log(htmlParser.parse(html, rule))


    rule = htmlParser.composeO({
        'title': htmlParser.textBy('h1.page-title'),
        'desc': htmlParser.textBy('.recipe-show .desc'),
        'cover_img': htmlParser.attrBy('div.cover.image img', 'src'),
        'author_source_id': htmlParser.attrBy('.author a', 'href'),
        'author_name': htmlParser.attrBy('.author a', 'title'),
        'composition': htmlParser.mapListBy('.ings tr', htmlParser.composeL(htmlParser.textBy('td.name'), htmlParser.textBy('td.unit'))),
        'step_title': htmlParser.textBy('h2#steps'),
        'step_list': htmlParser.mapListBy('.steps .container', htmlParser.composeO({text: htmlParser.textBy('.text'), img: htmlParser.attrBy('img', 'src')}))
    })
    console.log(htmlParser.parse(html, rule))
}

function mainTest2() {
    const html3 = `<div><td class="unit">500ml</td></div>`
    const $ = cheerio.load(html3, {xmlMode: true})
    console.log($('td.unit').text())
}


mainTest()